<style lang="less">
  @import '../../styles/common.less';
</style>

<template>
<div style="width: 480px;">
  <Form ref="thesisData" :model="thesisData" :rules="ruleValidate" :label-width="110">
    <FormItem label="论文名称：" prop="thesisName">
      <Input v-model="thesisData.thesisName" placeholder="请输入论文名称"></Input>
    </FormItem>
    <FormItem label="论文附件：" prop="files">
      <Row type="flex" justify="center" align="middle" class="height-100">
        <Upload
          action="/school/fileUpload/"
          :on-format-error="handleFormatError" :on-success="handleSuccess" :on-remove="handleRemove">
          <span>选择文件上传&nbsp;&nbsp;</span>
          <Button type="ghost" icon="ios-cloud-upload-outline">上传文件</Button>
        </Upload>
      </Row>
    </FormItem>
    <FormItem label="备注：">
      <Input v-model="thesisData.remark" placeholder="请输入备注"></Input>
    </FormItem>

    <FormItem>
      <Button type="primary" @click="handleSubmit('thesisData')">提交</Button>
      <Button type="ghost" @click="handleReset('thesisData')">清空</Button>
    </FormItem>
  </Form>
        </div>
</template>

<script>
  import moment from 'moment'
  export default {
    name: "thesismod",
    data(){
      return{
        thesisData:{
          thesisName:'',
          remark:'',
          thesisUrl: '',
          id:0
        },
        ruleValidate:{
          thesisName:[
            {required: true,message:'请输入论文名称',trigger:'blur'}
          ]
        }
      }
    },
    methods:{
      handleSubmit(name){
        this.$refs[name].validate((valid) => {
          if (valid) {
            let dataArr = this.thesisData;

            console.log(dataArr);
            if(this.thesisData.id > 0){
              this.$http.post('/school/updThesis', dataArr).then(res => {
                console.log(res);
                this.$Message.success('提交成功!');
                this.handleReset(name);
              });
            }else {
              this.$http.post('/school/addThesis', dataArr).then(res => {
                console.log(res);
                this.$Message.success('提交成功!');
                this.handleReset(name);
              });
            }
          }
        })
      },
      handleFormatError (file) {
        this.$Notice.warning({
          title: '文件格式不正确',
          desc: '文件 ' + file.name + ' 格式不正确，请选择文件。'
        });
      },
      handleSuccess(response, file, fileList){
        console.log(response);
        this.thesisData.thesisUrl = response.data[0];
      },
      handleRemove(file, fileList){
        this.thesisData.thesisUrl = '';
      },
      handleReset (name) {
        this.$refs[name].resetFields();
      }
    },
    mounted () {
      console.log(this.$route.query.id);
      if(this.$route.query.id == undefined) return;
      this.$http.get('/school/getThesisById?id='+this.$route.query.id).then(res=>{
        console.log(res);
        var dt = res.data.data;
        this.thesisData.id = dt.id;
        this.thesisData.thesisName = dt.thesisName;
        this.thesisData.remark = dt.remark;
        this.thesisData.thesisUrl = dt.thesisUrl;

      });
    },
    created () {

    }
  }
</script>

<style scoped>

</style>
